package com.lyxy.controller;


import com.lyxy.anno.MyLog;
import com.lyxy.pojo.Emp;
import com.lyxy.pojo.PageBean;
import com.lyxy.pojo.Result;
import com.lyxy.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/*员工管理*/
@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {


    @Autowired
    private EmpService empService;

    @MyLog
    @GetMapping
    public Result get(@RequestParam(required = false) String name,
                      @RequestParam(required = false) Short gender,
                      @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date begin,
                      @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") Date end,
                      @RequestParam(defaultValue = "1") Integer page,
                      @RequestParam(defaultValue = "10") Integer pageSize) {
     PageBean pageBean= empService.page( name,gender,begin,end,page, pageSize);
     return Result.success(pageBean);
    }

    @MyLog
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable("ids") List<Long> ids) {
        empService.delete(ids);
        return Result.success();
    }

    @MyLog
    @PostMapping
    public Result add(@RequestBody Emp emp) {
        empService.add(emp);
        return Result.success();
    }
    @MyLog
    @GetMapping("/{id}")
    public Result get(@PathVariable("id") Integer id) {
        Emp emp = empService.get(id);
        return Result.success(emp);
    }
    @MyLog
    @PutMapping
    public Result update(@RequestBody Emp emp) {
        empService.update(emp);
        return Result.success();
    }

}
